using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Acms.SqlDal;
    
namespace Acms.Bll
{
    public class ExceptionLog : BaseItem 
    {
        private ExceptionLog() : base() {}
        private ExceptionLog(Hashtable table) : base(table) { }
        private ExceptionLog(Hashtable table, List<ExceptionLog> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new ExceptionLogDal();
        }

        public static ExceptionLog CreateInstance()
        {
            return new ExceptionLog();
        }

        public static ExceptionLog CreateInstance(Exception e)
        {
            ExceptionLog tmp = new ExceptionLog();
            Exception eToLog = null;
            if (e is System.Web.HttpUnhandledException)
            {
                eToLog = e.InnerException;
            }
            else
            {
                eToLog = e;
            }
            tmp.exceptionType = eToLog.GetType().ToString();
            tmp.exceptionMessage = eToLog.Message;
            if (eToLog.InnerException != null)
            {
                tmp.innerExceptionType = eToLog.InnerException.GetType().ToString();
                tmp.innerExceptionMessage = eToLog.InnerException.Message;
            }
            tmp.source = eToLog.Source;
            tmp.stackTrace = eToLog.StackTrace;
            tmp.created = DateTime.UtcNow;
            return tmp;
        }

        public static ExceptionLog CreateInstance(System.Web.HttpContext context)
        {
            if (context != null)
            {
                ExceptionLog tmp = CreateInstance(context.Error);
                tmp.serverName = context.Request.ServerVariables["Server_Name"];
                tmp.applicationRoot = context.Request.ApplicationPath;
                tmp.url = context.Request.Url.ToString();
                tmp.rawUrl = context.Request.RawUrl;
                if (context.Request.QueryString["PostId"] != null)
                {
                    try
                    {
                        tmp.postId = int.Parse(context.Request.QueryString["PostId"]);
                    }
                    catch
                    {
                    }
                }
                if (context.Request.QueryString["SiteId"] != null)
                {
                    try
                    {
                        tmp.siteId = int.Parse(context.Request.QueryString["SiteId"]);
                    }
                    catch
                    {
                    }
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        private static ExceptionLog Get(Hashtable table)
        {
            if (table != null)
                return new ExceptionLog(table);
            else
                return null;
        }

        private static List<ExceptionLog> Get(Hashtable[] tables)
        {
            List<ExceptionLog> tmp = new List<ExceptionLog>();
            if (tables != null)
            {
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new ExceptionLog(table, tmp));
                }
            }
            return tmp;
        }

        public new void Update()
        {
            base.Update();
        }

        protected override object SaveChanges(ref object concurencyId, Base dal)
        {
            ((ExceptionLogDal)dal).Update(serverName, applicationRoot, url, rawUrl, postId, siteId, exceptionType, exceptionMessage, innerExceptionType, innerExceptionMessage, source, stackTrace, created, ref id);
            return id;
        }

        protected override void Load(Hashtable table)
        {
            id = (int)table[ExceptionLogDal.Fields.Id];
            serverName = (string)table[ExceptionLogDal.Fields.ServerName];
            applicationRoot = (string)table[ExceptionLogDal.Fields.ApplicationRoot];
            url = (string)table[ExceptionLogDal.Fields.Url];
            rawUrl = (string)table[ExceptionLogDal.Fields.RawUrl];
            postId = (int)table[ExceptionLogDal.Fields.PostId];
            siteId = (int)table[ExceptionLogDal.Fields.SiteId];
            exceptionType = (string)table[ExceptionLogDal.Fields.ExceptionType];
            exceptionMessage = (string)table[ExceptionLogDal.Fields.ExceptionMessage];
            innerExceptionType = (string)table[ExceptionLogDal.Fields.InnerExceptionType];
            innerExceptionMessage = (string)table[ExceptionLogDal.Fields.InnerExceptionMessage];
            source = (string)table[ExceptionLogDal.Fields.Source];
            stackTrace = (string)table[ExceptionLogDal.Fields.StackTrace];
            created = DateTime.SpecifyKind((DateTime)table[ExceptionLogDal.Fields.Created], DateTimeKind.Utc);
        }

        private int id;
        public int Id
        {
            get { return id;}
        }

        private string serverName;
        public string ServerName
        {
            get { return serverName; }
        }
	
        private string applicationRoot;
        public string ApplicationRoot
        {
            get { return applicationRoot; }
        }
	
        private string url;
        public string Url
        {
            get { return url; }
        }

        private string rawUrl;
        public string RawUrl
        {
            get { return rawUrl; }
        }

        private int postId;
        public int PostId
        {
            get { return postId; }
        }

        private int siteId;
        public int SiteId
        {
            get { return siteId; }
        }

        private string exceptionType;
        public string ExceptionType
        {
            get { return exceptionType; }
        }

        private string exceptionMessage;
        public string ExceptionMessage
        {
            get { return exceptionMessage; }
        }

        private string innerExceptionType;
        public string InnerExceptionType
        {
            get { return innerExceptionType; }
        }

        private string innerExceptionMessage;
        public string InnerExceptionMessage
        {
            get { return innerExceptionMessage; }
        }

        private string source;
        public string Source
        {
            get { return source; }
        }

        private string stackTrace;
        public string StackTrace
        {
            get { return stackTrace; }
        }
	
        private DateTime created;
        public DateTime Created
        {
            get { return created; }
        }

        public void LogToFile(string path)
        {
            System.IO.StreamWriter writer = new System.IO.StreamWriter(path, true);
            writer.WriteLine("**********************************************************************************");
            writer.WriteLine("Exception at {0}", created);
            writer.WriteLine("**********************************************************************************");
            writer.WriteLine("Server Name:\t{0}", serverName);
            writer.WriteLine("Application Root:\t{0}", applicationRoot);
            writer.WriteLine("Url:\t{0}", url);
            writer.WriteLine("Raw Url:\t{0}", rawUrl);
            writer.WriteLine("Post Id:\t{0}", postId);
            writer.WriteLine("Site Id:\t{0}", siteId);
            writer.WriteLine("Exception Type:\t{0}", exceptionType);
            writer.WriteLine("Exception Message:\t{0}", exceptionMessage);
            writer.WriteLine("Inner Exception Type:\t{0}", innerExceptionType);
            writer.WriteLine("Inner Exception Message:\t{0}", innerExceptionMessage);
            writer.WriteLine("Source:\t{0}", source);
            writer.WriteLine("Stack Trace:\t{0}", stackTrace);
            writer.WriteLine("**********************************************************************************");
            writer.Close();
        }
    }
}
